﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASoft.Base
{
    public class PageResult<T>
    {
        public PageResult(List<T> datas, int pageIndex, int pageSize, int dataCount)
        {
            this.Datas = datas;
            this.PageIndex = pageIndex < 1 ? 1 : pageIndex;
            this.PageSize = pageSize < 1 ? 20 : pageSize;
            this.DataCount = dataCount < 0 ? 0 : dataCount;

            this.PageCount = (DataCount / PageSize) + 1;
            this.PageIndex = PageIndex < PageCount ? PageIndex : PageCount;

            //处理最后一页
            if (PageIndex == PageCount && PageIndex != 1 && PageIndex * PageSize != DataCount)
            {
                var top = DataCount + PageSize - PageIndex * PageSize;
                this.Datas = datas.Take(top).ToList();
            }

        }

        public List<T> Datas { get; private set; }

        public int PageIndex { get; private set; }

        public int PageSize { get; private set; }

        public int PageCount { get; private set; }

        public int DataCount { get; private set; }
    }
}
